<template>
  <div>
    <!-- 面包屑导航栏 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>检测管理</el-breadcrumb-item>
      <el-breadcrumb-item>检测进度</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 进度步骤展示 -->
    <el-card>
      <h4>检测状态说明</h4>
      <el-steps :space="200" :active="1" finish-status="success">
        <el-step title="待检测" description="已经申请了仪器检测的初始状态"></el-step>
        <el-step title="检测中" description="正在对仪器进行检测中"></el-step>
        <el-step title="已检测(不)合格" description="仪器检测结果：合格/不合格"></el-step>
      </el-steps>
    </el-card>
    <!-- 卡片视图 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="5">
            <el-input placeholder="请输入部分仪器名称" v-model="queryInfo.query" clearable @clear="getProcessList">
            <el-button slot="append" icon="el-icon-search" @click="getProcessList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="3">
          <!-- clearable  可以删除 -->
          <el-select v-model="queryInfo.value1" placeholder="请选择提交人" >
            <el-option
              v-for="item1 in tijiaoren"
              :key="item1.user_id"
              :label="item1.user_id"
              :value="item1.user_id"
            >
            </el-option>
          </el-select>
        </el-col>
        <el-col :span="3">
          <el-select v-model="queryInfo.value2" placeholder="请选择送检单位" >
            <!-- sjdw   送检单位 -->
            <el-option
              v-for="item2 in sjdw"
              :key="item2.yiqi_songjiandanwei"
              :label="item2.yiqi_songjiandanwei"
              :value="item2.yiqi_songjiandanwei"
            >
            </el-option>
          </el-select>
        </el-col>
        <el-col :span="3">
          <el-select v-model="queryInfo.value3" placeholder="请选择制造商" >
            <el-option
              v-for="item3 in zhizaoshang"
              :key="item3.yiqi_zhizaoshang"
              :label="item3.yiqi_zhizaoshang"
              :value="item3.yiqi_zhizaoshang"
            >
            </el-option>
          </el-select>
        </el-col>
        <el-col :span="3">
          <el-select v-model="queryInfo.value4" placeholder="请选择检测状态" >
            <el-option
              v-for="item4 in jczt"
              :key="item4.yiqi_zhuangtai"
              :label="item4.yiqi_zhuangtai"
              :value="item4.yiqi_zhuangtai"
            >
            </el-option>
          </el-select>
        </el-col>
        <el-col :span="2">
          <el-button type="primary" @click="getProcessList">搜索</el-button>
        </el-col>
        <el-col :span="2">
          <el-button type="info" @click="clearValues">清除搜索</el-button>
        </el-col>
      </el-row>
    </el-card>
    <el-card>
      <!-- 搜索区域 -->
      <h2 style="text-align: center">检测申报记录列表</h2>
      <!-- 用户列表区域 -->
      <el-table :data="ProcessList" border stripe>
        <!-- stripe: 斑马条纹
        border：边框-->
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="yiqi_name" label="仪器名称"></el-table-column>
        <el-table-column prop="user_id" label="提交人"></el-table-column>
        <el-table-column prop="yiqi_songjiandanwei" label="送检单位"></el-table-column>
        <el-table-column prop="yiqi_zhizaoshang" label="制造商"></el-table-column>
        <el-table-column prop="yiqi_zhuangtai" label="检测状态"></el-table-column>
        <el-table-column prop="yiqi_time" label="更新时间"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-tooltip class="item" effect="dark" content="编辑" placement="top">
              <el-button
                type="primary"
                icon="el-icon-edit"
                size="mini"
                circle
                @click="showEditDialog(scope.row.yiqi_id)"
              ></el-button>
            </el-tooltip>
            <el-tooltip class="item" effect="dark" content="删除" placement="top">
              <el-button
                type="danger"
                icon="el-icon-delete"
                size="mini"
                circle
                @click="removeProcessById(scope.row.yiqi_id)"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[2, 5, 10, 15]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>

    <!-- 修改状态的对话框 -->
    <el-dialog
      title="修改流转状态"
      :visible.sync="editDialogVisible"
      width="50%"
      @close="editDialogClosed"
    >
      <!-- 内容主体 -->
      <el-form
        :model="editProcessForm"
        ref="editProcessFormRef"
        :rules="editProcessFormRules"
        label-width="70px"
      >
        <el-form-item label="仪器名称" label-width="10%">
          <el-input v-model="editProcessForm.yiqi_name" disabled></el-input>
        </el-form-item>
        <el-form-item label="提交人" label-width="10%">
          <el-input v-model="editProcessForm.user_id" disabled></el-input>
        </el-form-item>
        <el-form-item label="送检单位" label-width="10%">
          <el-input v-model="editProcessForm.yiqi_songjiandanwei" disabled></el-input>
        </el-form-item>
        <el-form-item label="制造商" label-width="10%">
          <el-input v-model="editProcessForm.yiqi_zhizaoshang" disabled></el-input>
        </el-form-item>
        <el-form-item label="检测状态" label-width="10%">
          <!--<el-input v-model="editProcessForm.yiqi_zhuangtai"></el-input>-->
          <template>
            <el-select v-model="value">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </template>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editProcess">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 修改流转状态选择框
      options: [{
        value: '待检测',
        label: '待检测'
      }, {
        value: '检测中',
        label: '检测中'
      }, {
        value: '已检测合格',
        label: '已检测合格'
      }, {
        value: '已检测不合格',
        label: '已检测不合格'
      }],
      // 绑定选择提交人下拉选择框信息
      tijiaoren: [
      ],
      // 绑定选择送检单位下拉选择框信息
      sjdw: [],
      // 绑定选择制造商下拉选择框信息
      zhizaoshang: [],
      // 绑定选择检测状态下拉选择框信息
      jczt: [],
      value: '',
      // 获取用户列表查询参数对象
      queryInfo: {
        value1: '',
        value2: '',
        value3: '',
        value4: '',
        query: '',
        // 当前页数
        pagenum: 1,
        // 每页显示多少数据
        pagesize: 5
      },
      // 所有检测列表
      ProcessList: [],
      // 编辑状态对话框
      editDialogVisible: false,
      // 编辑状态表单
      editProcessForm: {},
      editProcessFormRules: {},
      total: 0
    }
  },
  created () {
    this.getProcessList()
  },
  methods: {
    // 清空下拉列框中选中的值
    clearValues () {
      this.getProcessList()
      this.queryInfo.query = ''
      this.queryInfo.value1 = ''
      this.queryInfo.value2 = ''
      this.queryInfo.value3 = ''
      this.queryInfo.value4 = ''
    },
    // 删除用户
    async removeProcessById (id) {
      const confirmResult = await this.$confirm(
        '此操作将永久删除该记录, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(err => err)
      // 点击确定 返回值为：confirm
      // 点击取消 返回值为： cancel
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除')
      }
      const { data: res } = await this.$http.delete(
        'process/' + id)
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.$message.success('删除记录成功！')
      this.getProcessList()
    },
    async getProcessList () {
      const { data: res } = await this.$http.get('process', {
        params: this.queryInfo
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取检测列表失败！')
      }

      this.ProcessList = res.data.process
      this.total = res.data.total
      //
      // 获取用户名分组（不重复）
      const { data: res1 } = await this.$http.get('group/userid')
      if (res1.meta.status === 401) {
        return this.$message.error('获取提交人数据失败！无权限')
      }
      if (res1.meta.status !== 200) {
        return this.$message.error('获取提交人数据失败！')
      }

      this.tijiaoren = res1.data
      //
      // 获取送检单位分组（不重复）
      const { data: res2 } = await this.$http.get('group/danwei')
      if (res2.meta.status === 401) {
        return this.$message.error('获取送检单位数据失败！无权限')
      }
      if (res2.meta.status !== 200) {
        return this.$message.error('获取送检单位数据失败！')
      }

      this.sjdw = res2.data
      //
      // 获取制造商分组（不重复）
      const { data: res3 } = await this.$http.get('group/zhizaoshang')
      if (res3.meta.status === 401) {
        return this.$message.error('获取制造商数据失败！无权限')
      }
      if (res3.meta.status !== 200) {
        return this.$message.error('获取制造商数据失败！')
      }

      this.zhizaoshang = res3.data
      //
      // 获取检测状态分组（不重复）
      const { data: res4 } = await this.$http.get('group/zhuangtai')
      if (res4.meta.status === 401) {
        return this.$message.error('获取检测状态数据失败！无权限')
      }
      if (res4.meta.status !== 200) {
        return this.$message.error('获取检测状态数据失败！')
      }

      this.jczt = res4.data
    },
    // 监听 pagesize改变的事件
    handleSizeChange (newSize) {
      // console.log(newSize)
      this.queryInfo.pagesize = newSize
      this.getProcessList()
    },
    // 监听 页码值 改变事件
    handleCurrentChange (newSize) {
      // console.log(newSize)
      this.queryInfo.pagenum = newSize
      this.getProcessList()
    },
    // 监听修改对话框的关闭事件
    editDialogClosed () {
      this.$refs.editProcessFormRef.resetFields()
    },
    // 监听 查看检测进度 对话框的关闭事件
    seachDialogClosed () {
      this.$refs.seachProcessFormRef.resetFields()
    },
    // 修改信息
    editProcess () {
      this.editProcessForm.yiqi_zhuangtai = this.value
      // 提交请求前，表单预验证
      this.$refs.editProcessFormRef.validate(async valid => {
        // console.log(valid)
        // 表单预校验失败
        if (!valid) return
        const { data: res } = await this.$http.put(
          'process/' + this.editProcessForm.yiqi_id,
          {
            yiqi_zhuangtai: this.value
          }
        )
        // 隐藏添加用户对话框
        this.editDialogVisible = false
        if (res.meta.status === 200) {
          this.$message.success('更新用户信息成功！')
        }
        if (res.meta.status !== 200) {
          this.$message.error(res.meta.msg)
        }
        this.getProcessList()
      })
    },
    // 编辑状态信息
    async showEditDialog (id) {
      const { data: res } = await this.$http.get('process/' + id)
      if (res.meta.status === 401) {
        return this.$message.error('查询检测信息失败！无权限')
      }
      if (res.meta.status !== 200) {
        return this.$message.error('查询检测信息失败！')
      }
      this.editProcessForm = res.data
      this.value = this.editProcessForm.yiqi_zhuangtai
      this.editDialogVisible = true
    }
  }
}
</script>

<style scoped>
  .chaxun {
    margin-top: 15px;
  }
</style>
